import json
import xml.dom.minidom
import xml.etree.ElementTree as ET

import requests


def pretty_print_xml(element):
    # 将 ElementTree 转换为字符串
    rough_string = ET.tostring(element, 'utf-8')
    # 使用 minidom 进行格式化
    reparsed = xml.dom.minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")


# 需要修改 crmID CRM工单流水号
def __new_eoms_xml__():
    wsdl_url = "http://10.10.80.215:7501/services/EOMSProcessSheet?wsdl"
    # wsdl_url = "http://localhost:7501/services/EOMSProcessSheet?wsdl"
    headers = {'content-type': 'text/xml;charset=UTF-8',
               'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
               'SOAPAction': 'application/soap+xml;charset=utf-8'
               }

    with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/create.xml', encoding='utf-8') as xml_file:
    # with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/createNet.xml', encoding='utf-8') as xml_file:
        body = xml_file.read()

    response = requests.post(wsdl_url, data=body, headers=headers, verify=False)
    root = ET.fromstring(response.text)

    # 格式化打印xml
    pretty_xml = pretty_print_xml(root)
    print(pretty_xml)

    # 原始响应结果
    # print(response.text)

# 	<!--集客故障流程工单接口，建立集客故障工单-->
def __renew_eoms_process__():
    wsdl_url = "http://10.10.80.215:7501/services/EOMSProcessSheet?wsdl"
    # wsdl_url = "http://localhost:7501/services/CustomersAlarm?wsdl"
    headers = {'content-type': 'text/xml;charset=UTF-8',
               'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
               'SOAPAction': 'application/soap+xml;charset=utf-8'
               }

    with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/update.xml', encoding='utf-8') as xml_file:
        body = xml_file.read()

    response = requests.post(wsdl_url, data=body, headers=headers, verify=False)
    root = ET.fromstring(response.text)

    # 格式化打印xml
    pretty_xml = pretty_print_xml(root)
    print(pretty_xml)

    # 原始响应结果
    # print(response.text)


def __create_new_json__():
    # URL 地址
    # url = 'http://10.10.80.215:7501/eoms/complaintPersonal/newWorkSheet'
    url = 'http://10.10.80.215:7501/eoms/complaintPersonal/renewWorkSheet'
    # url = 'http://localhost:7135/api/v2/complaint/createSheet'
    # url = 'http://localhost:17471/api/v2/iot-complaint/inf/createSheet'

    # wrkfmShowSwftno 每次都要修改这个数
    with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/create.json', 'r',
    # with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/mockjson.json', 'r',
    # 物联网
    # with open('/Users/chipingyung/AquaProjects/python/xz/src/在线客服投诉_物联网投诉/派单/createNet.json', 'r',
              encoding='utf-8') as file:
        json_data: str = file.read()
    data = json.loads(json_data)

    # 发送 POST 请求
    response = requests.post(url, json=data)

    # 打印响应的内容
    if response.status_code == 200:
        response_data = json.loads(response.text)
        # 格式化JSON输出
        formatted_response_data = json.dumps(response_data, indent=4, ensure_ascii=False)
        # 将响应结果写入新文件
        # with open('/gs/data/response/事件/response.json', 'w',
        #           encoding='utf-8') as outfile:
        #     outfile.write(formatted_response_data)
        print(formatted_response_data)
    else:
        print(f"请求失败，状态码：{response.status_code}")

if __name__ == '__main__':
    # xml 派新工单
    # __new_eoms_xml__()
    # 重派
    # __renew_eoms_process__()
    __create_new_json__()
